/**
 * @description 专题管理控制器
 * @author 韩
 */

const dayjs = require("dayjs");
const SUBJECT = require("../../../model/context/subject/SUBJECT.MODEL")
const { getFilter } = require("../../../utils/setFilter");
const { randomUUID } = require("crypto");
const { deleteRecord } = require("./realtion_subject&article");

// 创建专题
const addSubject = async ({
    id = null, title, recommend, imgUrl
}) => {
    try {
        const params = {
            createDate: dayjs(),
            title, recommend, imgUrl
        }
        let addRes = null;
        if (id == null) {
            // 添加创建
            addRes = await SUBJECT.create({
                ...params,
                id: randomUUID(),
            })
        } else {
            // 更新修改
            addRes = await SUBJECT.findOneAndUpdate({ id: id }, {
                $set: {
                    ...params
                }
            }, { new: true })
        }

        if (addRes) {
            const { id, title, recommend, imgUrl } = addRes

            // deleteProductionImage({ imgList: discardImage })
            return {
                code: 0,
                msg: "添加成功",
                data: {
                    id, title, recommend, imgUrl
                }
            }
        }
    } catch (error) {
        return {
            code: "2000",
            message: "失败",
        }
    }

}

// 查询列表 
const querySubject = async ({ title, current, pageSize }) => {
    try {
        let filter = {
            title: title && { $regex: eval("/" + title + "/i") },//商品名模糊查询
        }
        filter = getFilter(filter)
        const list = await SUBJECT.find(filter).sort({ createDate: -1 }).skip((current - 1) * pageSize).limit(pageSize);
        const totalCount = await SUBJECT.find(filter).countDocuments();
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 1,
            msg: "查询出错了"
        }
    }
}

// 根据id查详情
const getSubjectDetail = async ({ id }) => {
    try {
        const res = await SUBJECT.findOne({
            id
        })
        const {  title, recommend, imgUrl, createDate } = res;
        return {
            code: 0,
            msg: "查询成功",
            data: {
                id: id, title, recommend, imgUrl, createDate
            }
        }
    } catch (error) {
        console.log(error)
        return {
            code: '0001',
            msg: "专题详情获取失败"
        }
    }


}

// 删除
const deltetSubject = async ({ id = undefined, action = "clear" }) => {
    try {

        let res = null;
        if (action === "delete") {
            // 逻辑删除
            res = await SUBJECT.findOneAndUpdate({ id: id }, {
                $set: {
                    deleteFlag: true
                }
            }, { new: true })
        } else {
            //物理删除
            res = await SUBJECT.deleteOne({
                id: { $in: id }
            });
        }
        if (res) {
            action === "clear" && await deleteRecord(
                { subjectId: id && { $in: id } }
            )
            return {
                code: 0,
                msg: `删除成功`
            }
        } else {
            return {
                code: 100,
                msg: `删除失败`
            }
        }
    } catch (error) {
        console.log("错误：", error)
    }
}





module.exports = {
    addSubject, querySubject, getSubjectDetail, deltetSubject
}